package wish.experimental;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

import evaluation.results.SizeFilter;


import wish.filters.HTMLTagStructureFilter;
import wish.filters.NumberOfNodesFilter;
import wish.filters.ScoreFunctionFilter;
import wish.filters.SimilarityFilter;
import wish.model.Record;
import wish.stages.DataExtractionStage;
import wish.stages.ParsingStage;
import wish.tagChecker.NumberTagChecker;

public class TestingFilters {

	public class WISH implements Runnable{

		private File wrapper;
		private File file;

		public WISH(File file, File wrapper) {
			
			this.wrapper = wrapper;
			this.file = file;
		}

		@Override
		public void run() {
			
			String html1Content;
			try {
				html1Content = ParsingStage.readFile(file);
				
				List<String> htmlContent = new ArrayList<String>();
				
				htmlContent.add(html1Content);
				
				List<Parser> list = new ParsingStage().ParseWebSite(htmlContent);
				
				for (Parser parser : list) {
					
					List<Record> records = new DataExtractionStage().findRecords(parser);
					
					List<Record> filtered = new ArrayList<Record>();
					
					for (Record record : records) {

						Record aux = new HTMLTagStructureFilter(3).filter(record);

						if (aux != null){
							filtered.add(aux);
						}
					}
					
					List<Record> filtered2 = new ArrayList<Record>();
					
					for (Record record : filtered) {
						
						
						Record aux = new SimilarityFilter(new NumberTagChecker(1, 1)).filter(record);
						
						
						if (aux != null)
							filtered2.add(aux);
					
					}
					
					List<Record> filtered3 = new ArrayList<Record>();
					
					for (Record record : filtered2) {
						
						Record aux = new NumberOfNodesFilter(3).exceedsNumberOfNodes(record);
						
						if (aux != null){
							filtered3.add(aux);
						}
						
					}
					
					ScoreFunctionFilter scf = new ScoreFunctionFilter(1, 15, 150, 50, 5);
					
					long max = Long.MIN_VALUE;
					
					Record finalRecord = null;
					
					for (Record record : filtered3) {
						
						long auxScore = scf.getScore(record);
						
						if (max < auxScore){
						
							max = auxScore;
							finalRecord = record;
							
						}
							
					}
					
					BufferedWriter bw = new BufferedWriter(new FileWriter(wrapper));
					
					bw.write(finalRecord.getNodeList().toHtml());
					
					bw.close();
				}
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
//			String html2 = "/home/pjbarrio/Documents/RelationExtractionStuff/DatabasesInterfaces/ResultsPage/2.html"
						
		}
		
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws ParserException 
	 */
	public static void main(String[] args) throws IOException, ParserException {
		
		new TestingFilters().start();
				
	}

	private void start() throws IOException {
		

		String type = "training";
		
		String out = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/WISH/" + type + "/";
		
		String results = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/QueryResults/" + type + "/";
		
		File sizeSorted = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/SizeSortedList/" + type + "/");
		
		int number = 1;
		
		File resFolder = new File(results);
		
		File[] databaseFolders = resFolder.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File arg0) {
				return arg0.isDirectory();
			}
		});
		
		for (int i = 0; i < databaseFolders.length; i++) {
			
			System.out.println("DATABASE: " + i);
			
			String name = databaseFolders[i].getName();
			
			File newfolder = new File(out + name);
			
			if (newfolder.exists())
				continue;
			
			newfolder.mkdir();
			
			File[] toWrap = databaseFolders[i].listFiles(new SizeFilter(new File(sizeSorted,name + ".list"),number));
			
			for (int j = 0; j < toWrap.length; j++) {
				
				System.gc();
				
				System.out.println("FILE: " + j);
				
				File outputFile = new File(newfolder,toWrap[j].getName());
				
				new TestingFilters().execute(toWrap[j],outputFile);
				
			}
			
		}
		
	}

	private void execute(File file, File wrapper) {
		
		Thread t = new Thread(new WISH(file,wrapper));
		
		t.start();
		
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
